import DEFAULT_SETTINGS from '@/settings';

// https://webpack.js.org/guides/dependency-management/#requirecontext
const resourceFiles = require.context('../../../public/纪委答题H5导图', true, /(\.png)|(.jpg)$/);

const host = DEFAULT_SETTINGS.oss_resources_host || './纪委答题H5导图/';

const preload_json = resourceFiles.keys().map(val => val.replace('./', host));

export default {
    namespaced: true,
    state: {
        host: host,
        resourcesCount: preload_json.length,
        loadedCount: 0,
        resources: {},
    },
    mutations: {
        SAVE_RESOURCES: (state, resources) => {
            state.resources = resources;
        },
        SET_LOADED_COUNT: (state, loadedCount) => {
            state.loadedCount = loadedCount;
        },
    },
    actions: {
        // 获取站点信息
        handlePreload({ state, commit }) {
            commit('SET_LOADED_COUNT', 0);
            preload_json.forEach((image) => {
                const img = new Image();
                img.onload = (evt) => {
                    // console.log(evt);
                    commit('SET_LOADED_COUNT', state.loadedCount + 1);
                };
                img.onerror = (evt) => {
                    // console.log(evt);
                };
                img.src = image;
            });
        },
    },
};
